package com.hulk.dryad.persistence.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.CacheConstants;
import com.hulk.dryad.persistence.entity.DictCode;
import com.hulk.dryad.persistence.entity.DictType;
import com.hulk.dryad.persistence.entity.enums.DictTypeEnum;
import com.hulk.dryad.persistence.mapper.DictCodeMapper;
import com.hulk.dryad.persistence.service.DictCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hulk
 * @since 2019-05-16
 */
@Service
public class DictCodeServiceImpl extends ServiceImpl<DictCodeMapper, DictCode> implements DictCodeService {



	@Autowired
	private DictTypeServiceImpl dictTypeServiceImpl;



	/**
	 * 删除字典项
	 * @param id 字典项ID
	 * @return R<Boolean>
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public R<Boolean> removeDictCode(Integer id) {
		// 根据ID查询字典ID
		DictCode dictCode = this.getById(id);
		DictType dict = dictTypeServiceImpl.getByType(dictCode.getDictType());
		// 系统内置
		if (DictTypeEnum.SYSTEM.getType().equals(dict.getRsv1())) {
			return R.failed("系统内置字典项目不能删除");
		}
		return R.ok(this.removeById(id));
	}

	/**
	 * 更新字典项
	 * @param code 字典项
	 * @return R<Boolean>
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public R<Boolean> updateDictCode(DictCode code) {
		// 查询字典
		DictType dict = dictTypeServiceImpl.getByType(code.getDictType());
		// 系统内置
		if (DictTypeEnum.SYSTEM.getType().equals(dict.getRsv1())) {
			return R.failed("系统内置字典项目不能删除");
		}
		return R.ok(this.updateById(code));
	}
}
